package com.fang.system.rpc.util;

import java.io.*;

/**
 * @author fanglingxiao
 * @version 1.0
 * @description TODO
 * @date 2022/7/17 12:46 上午
 **/
public class SerDerUtil {
    private static ByteArrayOutputStream out = new ByteArrayOutputStream();


    public synchronized static byte[] ser(Object msg) {
        out.reset();
        byte[] bytes = new byte[0];
        try {
            ObjectOutputStream oout = new ObjectOutputStream(out);
            oout.writeObject(msg);
            bytes = out.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bytes;
    }

    public static Object parse(byte[] bytes) {
        ByteArrayInputStream ipt = new ByteArrayInputStream(bytes);
        try {
            ObjectInputStream oipt = new ObjectInputStream(ipt);
            return oipt.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
